package com.jack.helloworld.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.jack.helloworld.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    GridView gridView;
    SimpleAdapter simpleAdapter;
    List<Map<String, Object>> listData;

    String[] types = new String[]{"iv", "tv"};
    int[] keys = new int[]{R.id.imageView, R.id.textView};

    String[] titles = new String[]{"QQ", "wechat", "pay", "settings",
            "t1", "t2", "t3", "t4", "t5", "t6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);

        initView();
        initData();
    }

    private void initView() {
        gridView = (GridView) findViewById(R.id.gridView);
    }

    private void initData() {
        listData = new ArrayList<Map<String, Object>>();
        simpleAdapter = new SimpleAdapter(this, getData(), R.layout.grad_view_item, types, keys);
        gridView.setAdapter(simpleAdapter);

        gridView.setOnItemClickListener(this);
    }

    private List<Map<String, Object>> getData() {
        for (int i = 0; i < titles.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("iv", R.mipmap.ic_launcher);
            map.put("tv", titles[i]);
            listData.add(map);
        }

        return listData;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        Toast.makeText(this, "click " + titles[position], Toast.LENGTH_SHORT).show();

    }
}
